package cn.devices.mapper;

import cn.devices.dto.UserAuthDto;
import cn.devices.entity.TbAuthUser;
import cn.devices.model.TbDmModel;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 设备个人授权表 Mapper 接口
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Repository
public interface TbAuthUserMapper  {

    @Insert("insert into tb_auth_user(device_code, user_id, authorize_time) " +
            "value (#{deviceCode},#{userId},#{authorizeTime})")
    int tbAuthUserAdd(TbAuthUser tbAuthUser);

    @Delete("delete from tb_auth_user where device_code=#{deviceCode} and user_id=#{userId}")
    int tbAuthUserDelete(String deviceCode,String userId);


    @Select("select tu.user_id,\n" +
            "       tu.user_name,\n" +
            "       tda.*,\n" +
            "       CONCAT('[',\n" +
            "              GROUP_CONCAT(\n" +
            "                      CONCAT('{\"device_code \":\"', tau.device_code, '\"'), ',',\n" +
            "                      CONCAT('\"authorize_time\":\"', tau.authorize_time, '\"'), '}')\n" +
            "           , ']')\n" +
            "           AS tbAuthUserList\n" +
            "from tb_user tu\n" +
            "         left join (SELECT td.*,\n" +
            "                           CONCAT('[',\n" +
            "                                  GROUP_CONCAT(\n" +
            "                                          CONCAT('{\"type_id\":\"', tt.type_id, '\"'), ',',\n" +
            "                                          CONCAT('\"type_name\":\"', tt.type_name, '\"'), ',',\n" +
            "                                          CONCAT('\"type_level\":\"', tt.type_level, '\"'), '}')\n" +
            "                               , ']')\n" +
            "                               AS tbAuthDmList\n" +
            "                    from tb_dm td\n" +
            "                             left join tb_auth_dm tad on td.dep_code = tad.dep_code\n" +
            "                             left join tb_type tt on tad.type_id = tt.type_id\n" +
            "                    group by td.dep_code) tda\n" +
            "                   on tu.dep_code = tda.dep_code\n" +
            "         left join tb_auth_user tau on tu.user_id = tau.user_id\n" +
            "group by tu.user_id")
    List<UserAuthDto> userAuthDtoList();

    @Select("select * from tb_auth_user where user_id=#{userId}")
    List<TbAuthUser> userAuthUserById(String userId);

    @Select("select tu.user_id,tu.user_name,tu.dep_code,td.dep_name from tb_user tu left join tb_dm td on tu.dep_code = td.dep_code where tu.disabled=0 order by dep_code\n")
    List<UserAuthDto> userList();

    @Select("select tau.* from tb_user tu inner join tb_auth_user tau on tu.user_id = tau.user_id where disabled=0\n")
    List<TbAuthUser> authUserList();

    @Select("select td.*,tad.type_id,tt.type_name,tt.type_level from tb_dm td left join tb_auth_dm tad on td.dep_code = tad.dep_code\n" +
            "left join tb_type tt on tad.type_id = tt.type_id")
    List<TbDmModel> tbDmModel();

}
